<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '.././mixins/resize'
import {
  getSevenDaysCust
} from "@/api/crmsys/dashboard/dashboard";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null,
      sevenDays: null,
      sevenDaysOther: null,
      sevenDaysTotal: null,
    }
  },
  mounted() {
  },
  beforeDestroy() {
  },
  created() {
    this.getSevenDaysCust();
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    },
    setOptions() {
      this.chart.setOption({
        xAxis: {
          data: this.sevenDays,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          type: 'value',
          minInterval: 1,
          axisTick: {
            show: false
          }
        },
        legend: {
          data: ['总客户数', '（3，4，5）星级客户']
        },
        series: [{
          name: '总客户数', itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: this.sevenDaysTotal,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
        {
          name: '（3，4，5）星级客户',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: this.sevenDaysOther,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }]
      })
    },
    getSevenDaysCust() {
      getSevenDaysCust().then(response => {
        this.sevenDays = response.data.sevenDays;
        for(let item in this.sevenDays){
          this.sevenDays[item]=this.sevenDays[item].substring(5);
        } 
        this.sevenDaysOther = response.data.sevenDaysOther;
        this.sevenDaysTotal = response.data.sevenDaysTotal;
        this.initChart();
      })
    }
  }
}
</script>
